﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using HtmlAgilityPack;
using System.IO;
using System.Xml.Linq;
using Newtonsoft.Json.Linq;

namespace Lot.WebClient.Response
{
    public class WebClientResponse
    {
        public Boolean Success { get; set; }

        public HttpWebResponse HttpResponse { get; set; }

        public WebException Exception { get; set; }

        public HtmlDocument AsHtml(Encoding encodingOverride = null)
        {
            HtmlDocument returnObject = null;

            if (this.Success)
            {
                try
                {
                    Encoding encoding = null;
                    using (var stream = HttpRequestEngine.GetResponseStream(this.HttpResponse, out encoding))
                    {
                        HtmlDocument doc = new HtmlDocument();
                        
                        stream.Position = 0;

                        if (encodingOverride != null)                        
                            encoding = encodingOverride;   


                        if (encoding != null)
                            doc.Load(stream, encoding);
                        else
                            doc.Load(stream);

                        if (doc.DeclaredEncoding != null && (encoding == Encoding.GetEncoding("ISO-8859-1") || encoding == null))
                            if (doc.DeclaredEncoding != encoding)
                            {
                                stream.Position = 0;
                                encoding = doc.DeclaredEncoding;
                                doc.Load(stream, encoding);
                            }

                        returnObject = doc;
                    }
                }
                catch (Exception ex)
                {
                    
                }

            }

            return returnObject;
            
        }

        public XDocument AsXml(Encoding encodingOverride=null)
        {
            XDocument returnObject = null;

            if (this.Success)
            {
                try
                {                   
                    var responseString = HttpRequestEngine.GetResponseString(this.HttpResponse, encodingOverride);
                    XDocument returnDocument = XDocument.Parse(responseString);
                    returnObject = returnDocument;
                }
                catch (Exception ex)
                {
                    
                }

            }

            return returnObject;
        }

        public JObject AsJson()
        {

            JObject returnObject = null;

            if (this.Success)
            {
                try
                {
                    var responseString = HttpRequestEngine.GetResponseString(this.HttpResponse);
                    JObject json = JObject.Parse(responseString);
                    returnObject = json;
                }
                catch (Exception ex)
                {
                    
                }

            }

            return returnObject;

        }

        public String AsString(Encoding encodingOverride = null)
        {
            String returnObject = null;

            if (this.Success)
            {
                try
                {
                    returnObject = HttpRequestEngine.GetResponseString(this.HttpResponse, encodingOverride);                 
                }
                catch (Exception ex)
                {

                }

            }

            return returnObject;
        }

        public byte[] AsByteArray()
        {
            byte[] returnObject = null;

            if (this.Success)
            {
                try
                {                                        
                    using(var stream = this.AsMemoryStream())                   
                    {
                        returnObject = stream.ToArray();
                    }                                       

                }
                catch (Exception ex)
                {

                }

            }

            return returnObject;


        }

        public MemoryStream AsMemoryStream()
        {
            MemoryStream returnObject = new MemoryStream();

            if (this.Success)
            {
                try
                {
                    Encoding outEncoding;

                    using (var stream = HttpRequestEngine.GetResponseStream(this.HttpResponse, out outEncoding))                    
                        stream.CopyTo(returnObject);                        

                }
                catch (Exception ex)
                {
                    returnObject = null;
                }

            }

            return returnObject;
            

        }
    }

    //public class WebClientResponse<T> : WebClientResponse
    //{
    //    public T Response { get; set; }

    //    public Exception TranslateExpection { get; set; }

    //    public WebClientResponse()
    //    {
    
    //    }

    //    public WebClientResponse(WebClientResponse httpResponse)
    //    {
    //        this.Exception = httpResponse.Exception;
    //        this.HttpResponse = httpResponse.HttpResponse;
    //        this.Success = httpResponse.Success;
    //    }

    //}
}
